<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="录入追踪信息" v-model="visible" align-center width="800px" :z-index="2000" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top">
      <div class="grid grid-cols-2 gap-x-3">
        <el-form-item label="司机" prop="driver_name" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.driver_name" placeholder="司机" class="w-full" />
        </el-form-item>
        <el-form-item label="电话" prop="phone" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.phone" placeholder="电话" class="w-full" />
        </el-form-item>
        <el-form-item label="车牌号" prop="car_license" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.car_license" placeholder="车牌号" class="w-full" />
        </el-form-item>
        <el-form-item label="柜号/so" prop="cabinet_number"
          :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
          <el-input v-model="form.cabinet_number" placeholder="柜号/so" class="w-full" />
        </el-form-item>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="submit">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { nextTick } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const load = ref(false)
const visible = ref(false)
const form = ref({})
const formRef = ref()
let loadings = reactive({})

const emit = defineEmits(['close', 'success'])

// 打开弹窗
const open = async (wr_id) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  formRef?.value?.resetFields()
  form.value = { wr_id }
  loadings = reactive({})
  visible.value = true
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const submit = async () => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings.submit = true
  api.inputTrace(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>